package cn.com.generaldata.os.service.indicator_evaluation.industry_structure_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 主要海洋产业经济弹性系数评估指标计算类
 */
public class MajorMarineIndustryEconomicElasticCoefficientCalculateService extends IndicatorEvaluationCalculateService {

	private String addValueOfMajorMarineIndustryName = ProvinceIndicatorConstant.MAJOR_MARINE_ADDVALUE_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = Double.valueOf(0);
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			result = calculateResult(indicator, indicatorAccountVO);
		}
		return result;
	}

	private Double calculateResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double currentAddValueOfIndustry = getAddValueOfIndustry(indicatorAccountVO, indicator);
		Double currentAddValueOfMajorMarineIndustry = getAddValueOfMajorMarineIndustry(indicatorAccountVO);

		IndicatorAccountVO lastVO = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		lastVO.setYear(indicatorAccountVO.getYear() - 1);

		Double lastAddValueOfIndustry = getAddValueOfIndustry(lastVO, indicator);
		Double lastAddValueOfMajorMarineIndustry = getAddValueOfMajorMarineIndustry(lastVO);

		Validate.isTrue(!(Double.valueOf(0).equals(lastAddValueOfIndustry)));
		Validate.isTrue(!(Double.valueOf(0).equals(lastAddValueOfMajorMarineIndustry)));
		// if (Double.valueOf("0").equals(lastAddValueOfIndustry)) {
		// return Double.valueOf("0");
		// }
		// if (Double.valueOf("0").equals(lastAddValueOfMajorMarineIndustry)) {
		// return Double.valueOf("0");
		// }
		return (currentAddValueOfIndustry / lastAddValueOfIndustry)
				/ (currentAddValueOfMajorMarineIndustry / lastAddValueOfMajorMarineIndustry);
	}

	private Double getAddValueOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {
		String industryName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.get(indicator
				.getIndustryCode());

		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(), industryName);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

	/**
	 * 获得省级主要海洋产业的增加值
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getAddValueOfMajorMarineIndustry(IndicatorAccountVO indicatorAccountVO) {

		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(),
				addValueOfMajorMarineIndustryName);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

}
